{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n\ncross runtime compilation limitations:\nThe cross compile and saved model can only be loaded in Windows, it can no longer be loaded in Linux\nThe cross compile and saved model can only be loaded in the same Compute Capability as the Linux which it was cross compiled\n(for example, if the model was cross compiled in Linux with GeForceRTX 4080 which has Compute Capability of 8.9,\nIt cannot be loaded in Windows with GeForceRTX 3080 which has Compute Capability of 8.6)\n\n# Cross runtime compilation for windows example\n\nCompile and save the Resnet Model using Torch-TensorRT in Linux:\n\npython examples/dynamo/cross_runtime_compilation_for_windows.py --path trt_resnet.ep\n\nLoad the Resnet Model saved in Windows:\n\npython examples/dynamo/cross_runtime_compilation_for_windows.py --path trt_resnet.ep --load True\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Imports and Model Definition\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import argparse\nimport platform\n\nimport torch\nimport torch_tensorrt as torchtrt\nimport torchvision.models as models\n\nPARSER = argparse.ArgumentParser(\n    description=\"Cross runtime comilation for windows example: Resnet Model\"\n)\nPARSER.add_argument(\n    \"--load\", default=False, type=bool, required=False, help=\"Load the model in Windows\"\n)\nPARSER.add_argument(\n    \"--path\",\n    type=str,\n    required=True,\n    help=\"Path to the saved model file\",\n)\n\nargs = PARSER.parse_args()\ntorch.manual_seed(0)\nmodel = models.resnet18().cuda().eval()\ninput = torch.rand((1, 3, 224, 224)).to(\"cuda\")\ninputs = [input]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "According to the argument, it is either cross compile and save resnet model for windows in Linux\nor load the saved resnet model in Windows\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "if args.load:\n    # load the saved model in Windows\n    if platform.system() != \"Windows\" or platform.machine() != \"AMD64\":\n        raise ValueError(\n            \"cross runtime compiled model for windows can only be loaded in Windows system\"\n        )\n    loaded_model = torchtrt.load_cross_compiled_exported_program(args.path).module()\n    print(f\"model has been successfully loaded from ${args.path}\")\n    # inference\n    with torch.no_grad():\n        trt_output = loaded_model(input)\n    print(f\"inference result: {trt_output}\")\nelse:\n    if platform.system() != \"Linux\" or platform.architecture()[0] != \"64bit\":\n        raise ValueError(\n            \"cross runtime compiled model for windows can only be compiled in Linux system\"\n        )\n    compile_spec = {\n        \"min_block_size\": 1,\n    }\n    torchtrt.cross_compile_for_windows(\n        model, file_path=args.path, inputs=inputs, **compile_spec\n    )\n    print(\n        f\"model has been successfully cross compiled and saved in Linux to {args.path}\"\n    )"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.11.14"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}